import Toast from 'tdesign-miniprogram/toast/index';
var isSend = false
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mode: '',
    datetime_begin_visible: false,
    datetime_end_visible: false,
    datetime_begin: new Date().getTime(),
    datetime_end: new Date().getTime(),
    datetime_beginText: '',
    datetime_endText: '',
    schedule: {
      title: '',
      details: '',
      date: new Date().getTime(),
      begin_date: '',
      end_date: '',
      status: 1,
      type: '',
      flag: 'add'
    }
  },
  inputChange(e) {
    const {
      key
    } = e.currentTarget.dataset;
    let inputValue = e.detail.value
    this.setData({
      [`schedule.${key}`]: inputValue
    })
    console.log(key + ':', this.data.schedule[`${key}`])
  },
  showPicker(e) {
    const {
      mode
    } = e?.currentTarget?.dataset;
    this.setData({
      mode,
      [`${mode}_visible`]: true,
    });
  },
  hidePicker() {
    const {
      mode
    } = this.data;
    this.setData({
      [`${mode}_visible`]: false,
    });
  },
  onConfirm(e) {
    const {
      value
    } = e?.detail;
    const {
      mode
    } = this.data;
    // const { key } = e?.currentTarget?.dataset;

    // console.log('confim', value);
    console.log('mode', mode)
    // console.log('value',value)
    let date = mode == 'datetime_begin' ? 'begin_date' : 'end_date'
    this.setData({
      [mode]: value,
      [`${mode}Text`]: value,
      [`schedule.${date}`]: value,
    });

    this.hidePicker();
  },
  showText() {


  },
  settingSechdule() {
    let schedule = this.data.schedule
    let msg = ''
    if(schedule.title == '') {
      msg = '请填写标题'
    }else if(schedule.begin_date == ''){
      msg = '请选择开始日期'
    }else if(schedule.end_date == ''){
      msg = '请选择结束日期'
    }
    schedule['date'] = new Date().getTime()
    if(msg != ''){
      return  Toast({
        context: this,
        selector: '#t-toast',
        message: msg,
        theme: 'error',
      });
    }
    if (isSend) return
    isSend = true
    let _this = this
    if (schedule.flag == undefined || schedule.flag == null) schedule['flag'] = update
    wx.cloud.callFunction({
      name: 'updateSchedule',
      data: schedule
    }).then(res => {
      Toast({
        context: this,
        selector: '#t-toast',
        message: schedule.flag == 'add' ? `设置成功` : `日程已更新`,
        theme: 'success',
      });

      setTimeout(() => {
        wx.navigateBack({
          delta: 1
        });
        isSend = false
      }, 500)
      console.log('日程设置', res)
    }).catch(err => {
      console.error(err)
    })
  },
  onColumnChange(e) {
    console.log('pick', e?.detail?.value);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let updateSchdule = wx.getStorageSync('updateSchdule')
    let cntSchdule = wx.getStorageSync('cntSchdule')
    if (updateSchdule) {
      this.setData({
        schedule: {
          _id: cntSchdule._id,
          title: cntSchdule.title,
          details: cntSchdule.details,
          date: cntSchdule.date,
          begin_date: cntSchdule.begin_date,
          end_date: cntSchdule.end_date,
          status: cntSchdule.status,
          type: cntSchdule.type,
          flag: 'update'
        },
        datetime_beginText: cntSchdule.begin_date,
        datetime_endText: cntSchdule.end_date,
        datetime_begin: cntSchdule.begin_date,
        datetime_end: cntSchdule.end_date
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    isSend = false
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    wx.setStorageSync('updateSchdule', false)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})